package com.jfinal.kit;

import java.io.Serializable;

public class R<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	private int code;
	private boolean success;
	private String msg;
	private T data;

	private R(boolean success, int code, T data, String msg) {
		this.code = code;
		this.data = data;
		this.msg = msg;
		this.success = success;
	}

	public static <T> R<T> success() {
		return new R<T>(true, 200, null, "操作成功");
	}

	public static <T> R<T> success(String msg) {
		return new R<T>(true, 200, null, msg);
	}

	public static <T> R<T> successwithdata(T data) {
		return new R<T>(true, 200, data, "操作成功");
	}

	public static <T> R<T> error() {
		return new R<T>(false, 400, null, "操作失败");
	}

	public static <T> R<T> error(String msg) {
		return new R<T>(false, 400, null, msg);
	}

	public static <T> R<T> status(boolean flag) {
		return flag ? success("操作成功") : error("操作失败");
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String toString() {
		return "R(code=" + this.getCode() + ", success=" + this.isSuccess() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")";
	}

	public R() {
	}
}